//判断浏览器是否是谷歌浏览器，版本号是否满足需求
window.onload = function () {
	//check_chrome();
	del_session();
	
	//获取配置信息
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'config.json');
	xhr.onload = function() {
	  if (xhr.status === 200) {
	    var config = JSON.parse(xhr.responseText);
		console.log(config)
	    var siteName = config.siteName;
		var title1 = config.LoginTitleName1;
		var title2 = config.LoginTitleName2;
		var login_bg_img = config.Login_bg_img;
	    // 其他配置信息
	    document.title = siteName; // 设置网页标题为网站名称
		document.getElementById('sys_title').innerHTML=title1+'<br>'+title2;
		document.getElementById('bg_main').style.backgroundImage=login_bg_img;
	  }
	};
	xhr.send();
	
	//创建左右两侧的进度条元素
	const circle5 = document.querySelector('#circle5');
	
	for (let i = 0; i < 32; i++) {
	  const deg = 210 - i * 2;
	  const line = document.createElement('div');
	  line.classList.add('line_flash');
	  line.style.transform = `rotate(${deg}deg)`;
	  line.style.animation = `line_flash${i % 32 } 5s linear infinite`;
	  circle5.appendChild(line);
	}
	
	for (let i = 0; i < 32; i++) {
	  const deg = -30 + i * 2;
	  const line = document.createElement('div');
	  line.classList.add('line_flash');
	  line.style.transform = `rotate(${deg}deg)`;
	  line.style.animation = `line_flash${i % 32 } 5s linear infinite`;
	  circle5.appendChild(line);
	}
	
	//把焦点转到用户名输入框
	document.getElementById('yonghu').focus();
}

function del_session(){
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) { 
		}                     
	};	
	xmlhttp.open("POST", "./php/del_session.php", true);
	xmlhttp.withCredentials=true;
	xmlhttp.send(); 
}

//确定两个密码是否一致，一致的话进行跳转
function check_csh(ft){
	pwd1=document.getElementById('passwd').value;
	pwd2=document.getElementById('passwd_csh').value;
	if(pwd1!=pwd2){
		document.getElementById('login_message').innerHTML='密码不一致，请重新输入';
		document.getElementById('passwd').value="";
		document.getElementById('passwd_pos').value="";
		document.getElementById('passwd_csh').value="";
		document.getElementById('passwd_csh_pos').value="";
		document.getElementById('yonghu').value="";
		document.getElementById('input_mm').value="";
	}
	else{
		var xmlhttp = new XMLHttpRequest();
		var formd=new FormData();
		formd.append("newmm",pwd1);
		document.getElementById('login_message').innerHTML='正在修改密码';
		xmlhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) { 
				if(this.responseText=='success'){
					document.getElementById('login_message').innerHTML='修改成功，正在跳转...';
					//if(ft=='ft'){
						//ajax_jumppage('law','index.html');
					//}else{
						ajax_jumppage('main','index.html');
					//}
									
				}
			}                     
		};
		xmlhttp.open("POST", "./php/csh_mm.php", true);
		xmlhttp.withCredentials=true;
		xmlhttp.send(formd); 
	}
}
//模拟密码输入框，避免密码记忆
function input_passwd(event) {
  // 获取密码输入框
  const inputEl = document.getElementById('input_mm');
  // 获取密码隐藏框
  const passwdEl = document.getElementById('passwd');
  // 获取当前光标位置
  const pos = inputEl.selectionStart;
  // 获取当前输入的字符
  const char = event.key;
  
  if (event.keyCode === 8) {
    // 处理退格键情况
    if (pos > 0) {
      // 如果当前光标不在开头，则删除前一位密码
      const passwd = passwdEl.value.slice(0, pos - 1) + passwdEl.value.slice(pos);
      const inputStr = '*'.repeat(pos - 1) + inputEl.value.slice(pos - 1);
      passwdEl.value = passwd;
      inputEl.value = inputStr;
      inputEl.setSelectionRange(pos - 1, pos - 1);
    } else {
      // 当前光标在开头，删除隐藏框中的最后一位密码
      const passwd = passwdEl.value.slice(0, -1);
      passwdEl.value = passwd;
      inputEl.value = '';
    }
  } else {
    // 处理其他键的情况
    const passwd = passwdEl.value.slice(0, pos) + char + passwdEl.value.slice(pos);
    const inputStr = '*'.repeat(pos) + inputEl.value.slice(pos);
	
    passwdEl.value = passwd;
    inputEl.value = inputStr;
    inputEl.setSelectionRange(pos + 1, pos + 1);
  }
  
  // 在控制台输出密码框中的值
  console.log(passwdEl.value);
}




//登录成功后，将用户框改为新密码输入框，模拟密码输入框，避免密码记忆
function input_passwd2(input_v){
	if(window.event.keyCode==8){
		pos=document.getElementById('yonghu').selectionStart;
		arr=input_v.split("");
		arr_h=document.getElementById('passwd_csh').value.split("");
		passwd='';
		passwd_h='';

		if(document.getElementById('passwd_csh_pos').value!=0){
			for(var i=0;i<arr_h.length;i++){			
				if(i!=pos){
					passwd=passwd+arr_h[i];
					passwd_h=passwd_h+'*';		
				}	
			}
		}else{
			for(var i=0;i<arr_h.length;i++){
				passwd=passwd+arr_h[i];
				passwd_h=passwd_h+'*';			
			}
		}
			
		
		document.getElementById('yonghu').value=passwd_h;
		document.getElementById('passwd_csh').value=passwd;
	}else{
		arr=input_v.split("");
		arr_h=document.getElementById('passwd_csh').value.split("");
		passwd='';
		passwd_h='';
		focus_position=0;
		for(var i=0;i<arr.length;i++){			
			if(arr[i].indexOf('*')==-1){
				passwd=passwd+arr[i];
				focus_position=i+1;
		
			}
			if(arr_h.length>i){
				passwd=passwd+arr_h[i];
			}		
			passwd_h=passwd_h+'*';
		}	
		document.getElementById('yonghu').value=passwd_h;
		document.getElementById('passwd_csh').value=passwd;
		document.getElementById('yonghu').focus();
		if(focus_position!=0){
			document.getElementById('yonghu').setSelectionRange(focus_position,focus_position);
		}
	}
	document.getElementById('passwd_csh_pos').value=document.getElementById('yonghu').selectionStart;
}

//监听页面回车事件
document.onkeydown = function(event){
	var code = event.keyCode;
	if (code == 13) {
	   login();
	}	
}

//判断用户名密码是否正确
function login(){
	console.log(document.getElementById('passwd').value)
	var xmlhttp = new XMLHttpRequest();
	var formd=new FormData();
	
	yonghu=document.getElementById('yonghu').value;
	mima=document.getElementById('passwd').value;
	console.log(yonghu);
	console.log(mima);
	formd.append("yonghu",yonghu);
	formd.append("mima",mima);

	xmlhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) { 
			if(this.responseText=='success'){
				document.getElementById('login_message').innerHTML='登录成功，正在跳转...';
				ajax_jumppage('main','index.html');
			}
			else if(this.responseText=='first_time'){
				csh_mm('ft');
			}
			else if(this.responseText=='csh'){
				csh_mm('csh');
			}
			else{
				document.getElementById('login_message').innerHTML='用户名或密码错误';				
			}
		}                     
	};
	xmlhttp.open("POST", "./php/check_login.php", true);
	xmlhttp.withCredentials=true;
	xmlhttp.send(formd); 
}
//初始化密码
function csh_mm(ft){
	if(ft=='ft'){
		document.getElementById('login_message').innerHTML='首次登陆，请更改密码';
	}else{
		document.getElementById('login_message').innerHTML='请更改密码';
	}
	document.getElementById('yonghu').setAttribute("onkeyup","input_passwd2(this.value)");
	document.getElementById('yonghu').value='';
	document.getElementById('input_mm').value='';
	document.getElementById('passwd').value='';
	document.getElementById('yonghu').placeholder='请输入密码';
	document.getElementById('input_mm').placeholder='请再次输入密码确认';
	document.getElementById('confirm_button').innerHTML="<a onclick='check_csh(&quot;"+ft+"&quot;)' style='cursor: pointer;'>提&nbsp;&nbsp;&nbsp;&nbsp;交</a>"
}

	  